package com.etmvc.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.et.mvc.binding.BindingContext;
import com.et.mvc.binding.DataBinder;

public class DateBinder implements DataBinder{
    public Object bind(BindingContext ctx) throws Exception{
        //...
    	String data = ctx.getRequest().getParameter(ctx.getParameterName());
    	if (data == null || "".equals(data)) {
			return null;
		}
        try {
        	String fmt ;
        	if (data.length()<=10) {
				fmt = "yyyy-MM-dd";
			}else {
				fmt = "yyyy-MM-dd HH:mm:ss";
			}
			return new SimpleDateFormat(fmt).parse(data);
		} catch (ParseException e) {
			System.out.println(ctx.getParameterName()+" bind fail! value = "+data);
		}
		return null;
    }
}